<?php

namespace App\Models;


class Node extends Base
{
    //修改器 route_name RouteName
    public function setRouteNameAttribute($value)
    {
        //如果字段值为null，则设置为空字符串 修改添加生效
        $this->attributes['route_name'] = empty($value)?'':$value;
    }
    //访问器
    public function getMenuAttribute()
    {
        return $this->is_menu==1? '<span class="label label-danger radius">否</span>': '<span class="label label-success radius">是</span>';
    }
    //获取全部数据
    public function getAllList()
    {
        $data = self::get()->toArray();
        return $this->treeLevel($data);
    }

    //获取层级数据 $allow_node用户拥有的权限
    public function treeData($allow_node)
    {
        $query = self::where('is_menu','1');
        if(is_array($allow_node)){
            $query->whereIn('id',array_keys($allow_node));
        }
        $menuData = $query->get()->toArray();
        return $this->subTree($menuData);
    }
}
